﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace emlak
{
    public partial class frmEvTalepKayit : Form
    {
        public frmEvTalepKayit()
        {
            InitializeComponent();
        }
        snfTalepEvKayit TalepEvKayit = new snfTalepEvKayit();
        public void TalepEvAtama()
        {

            if (cmbIslemTuru.SelectedIndex == -1)
            { TalepEvKayit.IslemTuru = "-1"; }
            else { TalepEvKayit.IslemTuru = cmbIslemTuru.SelectedItem.ToString(); }

            TalepEvKayit.IlanId = Convert.ToInt32(tbIlanID.Text);
            TalepEvKayit.Musteri = tbMusteri.Text;

            if (cmbBinaTipi.SelectedIndex == -1)
            {
                TalepEvKayit.BinaTipi = "-1";
            }
            else
            {
                TalepEvKayit.BinaTipi = cmbBinaTipi.SelectedItem.ToString();
            }

            TalepEvKayit.Metrekare1 = Convert.ToDouble(tbMetreKare1.Text);
            TalepEvKayit.Metrekare2 = Convert.ToDouble(tbMetreKare2.Text);
            TalepEvKayit.Il = tbIl.Text;
            TalepEvKayit.Ilce = tbIlce.Text;
            if (cmbSemt.SelectedIndex == -1)
            {
                TalepEvKayit.SemtMahalle = "-1";
            }
            else
            {
                TalepEvKayit.SemtMahalle = cmbSemt.SelectedItem.ToString();
            }
            

            TalepEvKayit.BinaKat1 = Convert.ToInt32(tbBinaKat1.Text);
            TalepEvKayit.BinaKat2 = Convert.ToInt32(tbBinaKat2.Text);

            TalepEvKayit.Fiyat1 = Convert.ToDouble(tbFiyat1.Text);
            TalepEvKayit.Fiyat2 = Convert.ToDouble(tbFiyat2.Text);


            if (cmbDovizTuru.SelectedIndex == -1)
            {
                TalepEvKayit.DovizTuru = "-1";
            }
            else
            {
                TalepEvKayit.DovizTuru = cmbDovizTuru.SelectedItem.ToString();
            }

            if (cmbOdaSayisi1.SelectedIndex == -1)
            {
                TalepEvKayit.OdaSayisi1 = -1;
            }
            else
            {
                TalepEvKayit.OdaSayisi1 = cmbOdaSayisi1.SelectedIndex;
            }

            if (cmbOdaSayisi2.SelectedIndex == -1)
            {
                TalepEvKayit.OdaSayisi2 = -1;
            }
            else
            {
                TalepEvKayit.OdaSayisi2 = cmbOdaSayisi2.SelectedIndex;
            }

            if (cmbIsinmaTuru.SelectedIndex == -1)
            {
                TalepEvKayit.IsinmaTuru = "-1";
            }
            else
            {
                TalepEvKayit.IsinmaTuru = cmbIsinmaTuru.SelectedItem.ToString();
            }





        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            TalepEvAtama();
            snfVeritabani.EvTalep_Ekle_Guncelle(TalepEvKayit, 0);
        }

        private void btnGuncelle_Click(object sender, EventArgs e)
        {
            TalepEvAtama();
            snfVeritabani.EvTalep_Ekle_Guncelle(TalepEvKayit, TalepEvKayit.IlanId);

        }

        private void tbIlanID_Leave(object sender, EventArgs e)
        {
            if (tbIlanID.Text == "") return;

            try
            {
                DataTable dt = new DataTable();
                dt = Sorgu.EvTalepGetir_ID(Convert.ToInt32(tbIlanID.Text));
                tbBinaKat1.Text = dt.Rows[0]["binakat1"].ToString();
                tbBinaKat2.Text = dt.Rows[0]["binakat2"].ToString();
                tbFiyat1.Text = dt.Rows[0]["fiyat1"].ToString();
                tbFiyat2.Text = dt.Rows[0]["fiyat2"].ToString();
                tbIl.Text = dt.Rows[0]["il"].ToString().Trim();
                tbIlanID.Text = dt.Rows[0]["ilanID"].ToString();
                tbIlce.Text = dt.Rows[0]["ilce"].ToString().Trim();
                tbMetreKare1.Text = dt.Rows[0]["metrekare1"].ToString();
                tbMetreKare2.Text = dt.Rows[0]["metrekare2"].ToString();
                tbMusteri.Text = dt.Rows[0]["tc"].ToString();

                cmbBinaTipi.SelectedIndex = Convert.ToInt32(dt.Rows[0]["binatipi"].ToString());
                cmbDovizTuru.SelectedIndex = Convert.ToInt32(dt.Rows[0]["dovizkuru"].ToString());
                cmbIsinmaTuru.SelectedIndex = Convert.ToInt32(dt.Rows[0]["isinmasekli"].ToString());
                //cmbKategori.SelectedIndex = Convert.ToInt32(dt.Rows[0][""].ToString());
                string test = dt.Rows[0]["odasayisi1"].ToString();
                cmbOdaSayisi1.SelectedIndex = Convert.ToInt32(dt.Rows[0]["odasayisi1"].ToString());
                cmbOdaSayisi2.SelectedIndex = Convert.ToInt32(dt.Rows[0]["odasayisi2"].ToString());
                cmbSemt.SelectedIndex = Convert.ToInt32(dt.Rows[0]["semt"].ToString());


                btnKaydet.Enabled = false;
                btnSil.Enabled = true;
                btnGuncelle.Enabled = true;


            }
            catch
            {
                //MessageBox.Show("Böyle bir ID yok.");
            }
        }

        private void frmEvTalepKayit_Load(object sender, EventArgs e)
        {
            btnGuncelle.Enabled = false;
            btnSil.Enabled = false;
        }
    }
}
